<?php

namespace app\admin\controller\projects\shop;

use app\Base;
use app\service\admin\projects\shop\ClassifyService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\response\View;

/**
 * 分类管理
 * User : Du
 * Date : 2022/12/1
 * Time : 15:54
 * Class Classify
 * @package projects\controller\admin\shop
 */
class Classify extends Base
{

    /**
     * 分类管理
     * User : Du
     * Date : 2022/12/1
     * Time : 21:58
     * @return View
     */
    public function show(): View
    {
        return view('projects/shop/classify');
    }

    /**
     * 列表数据
     * User : Du
     * Date : 2022/12/1
     * Time : 19:24
     * @param ClassifyService $service
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList(ClassifyService $service): Json
    {
        $res = $service::getList($this->params);
        $this->res['data'] = $res['data'];
        $this->res['count'] = $res['count'];
        return $this->returnData();
    }

    /**
     * 添加分类
     * User : Du
     * Date : 2022/12/1
     * Time : 19:09
     * @param ClassifyService $service
     * @return Json
     */
    public function add(ClassifyService $service): Json
    {
        $res = $service::add($this->params);
        if (!$res) {
            return $this->returnData('addFail');
        }
        return $this->returnData();
    }

    /**
     * 修改分类
     * User : Du
     * Date : 2022/12/1
     * Time : 21:53
     * @param ClassifyService $service
     * @return Json
     * @throws DbException
     */
    public function update(ClassifyService $service): Json
    {
        $res = $service::update($this->params);
        if (!$res) {
            return $this->returnData('updateFail');
        }
        return $this->returnData();
    }

    /**
     * 删除分类
     * User : Du
     * Date : 2022/12/1
     * Time : 20:22
     * @param ClassifyService $service
     * @return Json
     * @throws DbException
     */
    public function delete(ClassifyService $service): Json
    {
        $res = $service::delete($this->params);
        if (!$res) {
            return $this->returnData('deleteFail');
        }
        return $this->returnData();
    }
}